<%@Page Language="VB"%>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>
<%@Import Namespace="System.Data.Common" %>
<%@ Register TagPrefix="khang" TagName="connect" Src="connect-strings.ascx" %>

<html><head>
<title>Writing Data from a DataSet to an XML File</title>
</head>
<body bgcolor="#ffffff">
<span class="heading">Writing Data from a DataSet to an XML File</span><hr />
<khang:connect id="ctlConnectStrings" runat="server" />

<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>SELECT command: <b><span id="outSelect" runat="server"></span></b></div><p />
<div id="outMessage" runat="server">&nbsp;</div>

<script language="VB" runat="server">

Sub Page_Load()

   Dim strConnect As String
   strConnect = ctlConnectStrings.SqlConnectionString
   outConnect.innerText = strConnect
   Dim strSelect As String
   strSelect = "SELECT * from tblSubCategories"
   outSelect.innerText = strSelect  
   Dim objDataSet As New DataSet
   Try
      Dim objConnect As New SqlConnection(strConnect)
      Dim objDataAdapter As New SqlDataAdapter(strSelect, objConnect)
      objDataAdapter.Fill(objDataSet, "Books")
   Catch objError As Exception
      	outMessage.innerHTML = _
	"<b>* Error while accessing data" & _
	" </b>.<br />" & _
	objError.Message & "<br />" & _
	objError.Source
      Exit Sub  
   End Try
   Try    
      Dim strVirtualPath As String = Request.ApplicationPath & "\XML-from-DataSet.xml"
      Dim strVSchemaPath As String = Request.ApplicationPath & "\Schema-from-DataSet.xsd"
      
      'write the data and schema from the DataSet to an XML document on disk
      'must use the Physical path to the file not the Virtual path
      objDataSet.WriteXML(Request.MapPath(strVirtualPath))
      outMessage.innerHTML = "Written file: <b><a href=" & Chr(34) & strVirtualPath _
                & Chr(34) & ">" & strVirtualPath & "</a></b><br />"
      objDataSet.WriteXMLSchema(Request.MapPath(strVSchemaPath))
      outMessage.innerHTML += "Written file: <b><a href=" & Chr(34) & strVSchemaPath _
                & Chr(34) & ">" & strVSchemaPath & "</a></b>"
   Catch objError As Exception
	outMessage.innerHTML = "<b>* " & _
	" Error while writing disk file</b>.<br />" & _
          	 objError.Message & "<br />" & _
	objError.Source
      Exit Sub 
   End Try
End Sub
</script>
</body>
</html>
